'use strict';

let nunjucks = require('nunjucks');

function createEnv(path, opts) {
    path = path || 'views';
    opts = opts || {};
    let nunjucksConfig = {
        IstrimBlocks: opts.IstrimBlocks || true,
        trimBlocks: opts.trimBlocks || true
    }
    let env = nunjucks.configure('views', nunjucksConfig);
    return env
}

let MiddleWare = async (ctx, next) => {
    let env = createEnv();
    ctx.render = (views,model) => {
        ctx.body = env.render(views,model);
    }
    await next();
}

module.exports = MiddleWare;